//
//  SDLevelDetailController.m
//  Sudoku
//
//  Created by wxlan on 16/6/13.
//  Copyright © 2016年 wxlan. All rights reserved.
//

#import "SDLevelDetailController.h"
#import "SDLevelDetailModel.h"
#import "SDLevelDetailCell.h"
#import "SDGameController.h"
@interface SDLevelDetailController()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray *leverDetailArray;
@end

@implementation SDLevelDetailController

- (NSMutableArray *)leverDetailArray
{
    if (_leverDetailArray == nil) {
        _leverDetailArray = [[NSMutableArray alloc] init];
    }
    return _leverDetailArray;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Level.plist" ofType:nil];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSArray *levelArray = dict[@"Easy"];
    [self.leverDetailArray addObjectsFromArray:[SDLevelDetailModel mj_objectArrayWithKeyValuesArray:levelArray]];
    [self buidlUI];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.leverDetailArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SDLevelDetailCell *cell = [SDLevelDetailCell levelDetailCellWithTableView:tableView];
    cell.levelDetailModel = self.leverDetailArray[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    [self.navigationController pushViewController:[[SDGameController alloc] initWithLevelDetailModel:self.leverDetailArray[indexPath.row]] animated:YES];

}

- (void)buidlUI
{
    self.view.backgroundColor = [UIColor blackColor];

    UIView *bottomView = [[UIView alloc] init];
    [self.view addSubview: bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.equalTo(@(44));
    }];
    
    UIButton *moreButton = [[UIButton alloc] init];
    [bottomView addSubview:moreButton];
    [moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.equalTo(bottomView);
        make.width.equalTo(@(50));
    }];
    UIView *separatorView = [[UIView alloc] init];
    separatorView.backgroundColor = [UIColor grayColor];
    [bottomView addSubview:separatorView];
    [separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(moreButton.mas_left);
        make.width.equalTo(@(1));
        make.height.equalTo(@(30));
        make.centerY.equalTo(bottomView.mas_centerY);
    }];
    
    UIButton *sortButton = [[UIButton alloc] init];
    [sortButton setTitle:@"排列方式" forState:UIControlStateNormal];
    [sortButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bottomView addSubview:sortButton];
    
    [sortButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(bottomView);
        make.right.equalTo(separatorView.mas_left);
    }];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
    tableView.backgroundColor = [UIColor blackColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.bottom.mas_equalTo(bottomView.mas_top);
    }];

}
@end
